import { Component, OnInit } from '@angular/core';
import { HttpService } from '../../.././services/http.service';
import { Md5 } from "ts-md5/dist/md5";
import { Router } from '@angular/router';
import { BaseUiService } from '../../../services/base-ui.service';

@Component({
  selector: 'app-forgetpwd',
  templateUrl: './forgetpwd.page.html',
  styleUrls: ['./forgetpwd.page.scss'],
})
export class ForgetpwdPage implements OnInit {

  constructor(
    public router: Router,
    public BaseUiService: BaseUiService,
    public HttpService: HttpService,
  ) { }
  //验证码倒计时 全局定义变量
  public verifyCode: any ={
    verifyCodeTips: "获取验证码",
    countdown: 60,//总共时间
    disable: true
  };
  public phoneNum:any
  public verifyNum:any
  public yzmCode:any = true
  public isShow:boolean =true
  public pwd1:any
  public pwd2:any
  ngOnInit() {

  }

   //获取验证码的方法
   getVerify(){
    //点击时判断有没有输入
    if(this.phoneNum==undefined){
      this.BaseUiService.showToast("手机号不能为空!",'customred','top');
    }else if(!(/^1[3456789]\d{9}$/.test(this.phoneNum))){
      this.BaseUiService.showToast("密码格式不正确,请输入6-16数字",'customred','top');
    }else{
    //每次点击时初始化
    this.verifyCode = {
        verifyCodeTips: "获取验证码",
        countdown: 60,//总共时间
        disable: true //禁止按钮被点击
    }
    this.settime()
      let p = {
      mobile:this.phoneNum,
      SmsCode: 1
    }
    this.HttpService.get_verify(p).then(res => {
      if(res["code"]==200){
        this.yzmCode=false
      }else{
        this.BaseUiService.showToast("获取验证码失败！",'customred','top');
      }
    })
  }
  }

  //倒计时
  settime() {
  if(this.verifyCode.countdown == 0) {
    this.verifyCode.verifyCodeTips = "获取验证码";
    this.verifyCode.disable = true;
    return;
  } else {
    this.verifyCode.countdown--;
    this.verifyCode.disable = false;
  }
  setTimeout(() => {
    this.verifyCode.verifyCodeTips = "重新获取" + this.verifyCode.countdown + "秒";
    this.settime();
  }, 1000);
  }

  //下一步验证
  goforget(){
    let p = {
      mobile: this.phoneNum,
      smsCode: this.verifyNum,
      terminalType:1
    }
    this.HttpService.forgetYzm(p).then(res =>{
      if(res["code"]==200){
        this.isShow = false
      }else{
        this.BaseUiService.showToast(res['msg'],'customred','top')
      }
    }).catch(err =>{
      this.BaseUiService.showToast(err,'customred','top')
    })
  }
  //修改密码
  goforgetpwd(){
    let pwdZ = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
    if(this.pwd1!==this.pwd2||!this.pwd1||!this.pwd2){
      return this.BaseUiService.showToast('密码不一致或不能为空','customred','top')
    }else if(!pwdZ.test(this.pwd1)){
      return this.BaseUiService.showToast('密码格式不正确,请输入6-16数字加字母','customred','top')
    }else{
      let p = {
        newPassword : Md5.hashStr(this.pwd2),
        mobile: this.phoneNum
      }
      this.HttpService.forgetPwd(p).then(res=>{
        if(res['code']==200){
          this.BaseUiService.showToast('修改成功！','medium','top')
          this.router.navigate(['/tabs/tab4'])
        }else{
          this.BaseUiService.showToast(res['msg'],'customred','top')
        }
      }).catch(err =>{
        this.BaseUiService.showToast(err,'customred','top')
      })
    }
  }
}
